package com.yfeng.goodjuhome.action.company;

import com.yfeng.exception.ActionException;
import com.yfeng.goodjuhome.model.User;
import com.yfeng.goodjuhome.service.INewsService;
import com.yfeng.goodjuhome.service.IProductService;
import com.yfeng.goodjuhome.service.IUserService;
import com.yfeng.goodjuhome.service.IVisitService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * www.goodjuhome.com
 * Created by IntelliJ IDEA.
 * User: rqn
 * Date: 2010-8-14
 * Time: 13:27:51
 */
@Controller("companyAction")
public class CompanyAction {

    @Autowired
    @Qualifier("newsService")
    private INewsService newsService;

    @Autowired
    @Qualifier("userService")
    private IUserService userService;

    @Autowired
    @Qualifier("productService")
    private IProductService productService;

    @Autowired
    @Qualifier("visitService")
    private IVisitService visitService;


    private final Log log = LogFactory.getLog(this.getClass());

    @RequestMapping("/home/e/index")
    public String toCompanyIndexPage(HttpServletRequest req, HttpSession session,ModelMap model) throws Exception {
        String uid = req.getParameter("uid");

        String selfuid=""; //访问者
        if(session.getAttribute("currUser")!=null)
        {
           selfuid=((User)session.getAttribute("currUser")).getId();
            visitService.addVisit(req,selfuid,uid); //添加访问者
        }

        if (StringUtils.isEmpty(uid))
            throw new ActionException("uid is null");
        if (!userService.isExist(uid))
            throw new ActionException("不存在的用户ID:" + uid);
        else {
            User user = userService.getUser(uid);

            List qyNewsList = newsService.queryNewsByUidAndType(uid,"12","8"); //12企业新闻    8条数据
            List ggNewsList = newsService.queryNewsByUidAndType(uid,"14","8"); //14 产品公告


             
            List productList = productService.queryProductListByUid(uid, "10");

            model.addAttribute("userobj", user);
            model.addAttribute("qyNewsList", qyNewsList);
            model.addAttribute("ggNewsList", ggNewsList);
            model.addAttribute("productList", productList);

            return "/user/qy/index";
        }
    }
}
